#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

"""
    一辆汽车行驶200km, 以匀速行驶, 但速度未知.
    速度的概率可能性为30km/h - 60km/h 中任意的值.

    模拟汽车全程时间的时间累积分布概率.
"""

from random import random
from numpy import arange


V1 = 30
V2 = 60
assert V2 > V1
V_SPAN = V2 - V1
DISTANCE = 200

experi_hour_results = []


def randfloat(begin, end):
    span = end - begin
    return random() * span + begin


def one_experiment():
    v = randfloat(V1, V2)
    t = DISTANCE/v
    experi_hour_results.append(t)


def statistics():
    t_left  = DISTANCE / V2
    t_right = DISTANCE / V1
    num = 10
    precision = (t_right - t_left)/num       # 平均分为10等分, 统计这10个数值的累积分布

    accu_frequence = [0] * num
    real = [0] * num
    for idx, t in enumerate(arange(t_left, t_right, precision)):
        real[idx] = 1/V_SPAN * (V2 - DISTANCE/t)
        print(t)
        for experiment_t in experi_hour_results:
            if experiment_t <= t:
                accu_frequence[idx] += 1

    print(real)
    accu_frequence = [f/len(experi_hour_results) for f in accu_frequence]
    print(accu_frequence)


def experiment():
    for i in range(10 ** 5):
        one_experiment()

    statistics()
    print(len(experi_hour_results))


def main():
    print("start main")
    experiment()

if __name__ == "__main__":
    main()
